#include "structures.hh"
#include <cmath>
#include <map>
#include <iostream>

unsigned int getCost ( 
    std::vector< Piece* > const& pieces,
    std::vector< Musician* > const& musicians) 
{
  std::map < unsigned int, unsigned int > start;
  std::map < unsigned int, unsigned int > stop;

  std::vector < Piece* >::const_iterator p;
  std::vector < Musician* >::const_iterator m;
  unsigned int time = 0;

  for ( p = pieces.begin(); p != pieces.end(); ++p ) {
    unsigned int newStop = time + (*p)->length;

    for ( m = (*p)->musicians.begin(); m != (*p)->musicians.end(); ++m ) {

      if ( start.count ( (*m)->id ) == 0 ) start[(*m)->id] = time;
      stop[(*m)->id] = newStop; 

    }

    time += (*p)->length;
  }

  unsigned int cost = 0;
  for ( m = musicians.begin(); m != musicians.end(); ++m )
    cost += (*m)->hourlyRate * (stop[(*m)->id] - start[(*m)->id]);

  return cost;
}

void display ( std::vector < Piece* > const& pieces,
    std::vector< Musician* > const& musicians )
{
  std::cout << getCost ( pieces, musicians ) << std::endl;

  std::vector < Piece* >::const_iterator p = pieces.begin();

  std::cout << (*p)->id; p++;
  for ( ; p != pieces.end(); ++p )
    std::cout << "," << (*p)->id;
  std::cout << std::endl;
}
